package com.freen.fadmin.entity.sysuser;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@Entity
@Table(name = "sys_permission",schema = "sys_permission")
//@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
//@JsonIgnoreProperties(value = { "userInfos" })
@JsonIgnoreProperties(value = { "roles" })
public class SysPermission implements Serializable {
    @Id
    @GeneratedValue
    private Integer id;
    @Column(name = "name",length = 20,nullable = false,unique = true)
    private String name;

    @Column(name = "cnname",length = 20)
    private String cnname;
    @Column(name = "resource_type",columnDefinition = "enum('menu','button')")
    private String resourceType;
    @Column(name = "url",length = 100)
    private String url;
    private String permission;

    @Column(name = "remark",length = 50)
    private String remark;
    @Column(name = "parent_id")
    private Integer parentId;
    @Column(name = "parent_ids")
    private String parentIds;
    private Boolean available = Boolean.FALSE;
    @Transient
    private List permissions;
    //@JsonBackReference
    @ManyToMany
    @JoinTable(name = "sys_role_permission",schema = "sys_role_permission",joinColumns = {@JoinColumn(name = "permission_id",columnDefinition = "INT(11)")},inverseJoinColumns = {@JoinColumn(name = "role_id",columnDefinition = "INT(11)")})
    private List<SysRole> roles;
    //@JsonBackReference
    public List getPermissions(){
        if (this.permission != null){
            return Arrays.asList(this.permission.trim().split("|"));
        }else {
            return null;
        }
    }
    //@JsonBackReference
    public void setPermissions(List permissions){
        this.permissions = permissions;
    }

    //@JsonBackReference
    public List<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCnname() {
        return cnname;
    }

    public void setCnname(String cnname) {
        this.cnname = cnname;
    }

    public String getResourceType() {
        return resourceType;
    }

    public void setResourceType(String resourceType) {
        this.resourceType = resourceType;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public String getParentIds() {
        return parentIds;
    }

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    public Boolean getAvailable() {
        return available;
    }

    public void setAvailable(Boolean available) {
        this.available = available;
    }

}
